package com.zq.api.task;


import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zq.common.constant.OrderConstants;
import com.zq.common.constant.OrderStatusConstants;
import com.zq.server.api.dao.OrdersMapper;
import com.zq.server.api.domain.entity.ApiOrders;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Super
 * 处理定时任务
 */
@Component
@Slf4j
public class orderTask {

    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 两分钟执行一次处理超时订单
     */
    @Scheduled(cron = " 0 */1 * * * ? ")
    public void orderTaskANDTime() {
        log.info("超时订单处理");
        // 当前时间减去15分钟
        LocalDateTime pastTime = LocalDateTime.now().plusMinutes(-15);
        // 查询超时订单
        List<ApiOrders> orders = ordersMapper.CheckOrderTimeoutCheck(OrderStatusConstants.STAY_PAY, pastTime);
        /**
         * 取消订单
         * 取消原因
         * 取消时间
         */
        for (ApiOrders order : orders) {
            order.setStatus(OrderStatusConstants.OVER_CANCEL);
            order.setCancelTime(LocalDateTime.now());
            order.setCancelReason(OrderConstants.ORDER_OVERTIME);
            log.info("订单取消:{}", order.getId());
            ordersMapper.update(order);
        }
    }
}
